package com.gwtgeo.ui.client.mvp.layers;

import java.util.Map;

import com.gwtgeo.service.model.WMSLayerDef;
import com.gwtgeo.ui.client.mvp.HasValues;
import com.gwtgeo.ui.client.mvp.adapter.HasValuesListGridAdapter;
import com.gwtgeo.ui.client.mvp.windowsmanager.WindowedDisplay;
import com.gwtgeo.ui.client.mvp.windowsmanager.WindowedView;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.DragDataAction;
import com.smartgwt.client.types.HeaderControls;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.HasClickHandlers;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.HasCellClickHandlers;
import com.smartgwt.client.widgets.grid.events.HasChangedHandlers;
import com.smartgwt.client.widgets.grid.events.HasRecordDropHandlers;
import com.smartgwt.client.widgets.layout.VLayout;

public class LayersView extends WindowedView<VLayout> implements LayersPresenter.Display {

	private ListGrid layersGrid;

	private ListGridField layerSelectedField;
	
	private IButton addLayerButton;

	public LayersView() {
		super();
	}

	@Override
	public HasChangedHandlers getLayerVisibilityChange() {
		return layerSelectedField;
	}

	@Override
	public HasRecordDropHandlers getLayerReposition() {
		return this.layersGrid;
	}

	@Override
	protected VLayout buildWidget() {
		
		VLayout widget = new VLayout();

		this.layersGrid = new ListGrid();

		this.layersGrid.setCanRemoveRecords(true);
		this.layersGrid.setWidth100();
		this.layersGrid.setHeight100();

		this.layersGrid.setCanDragRecordsOut(true);
		this.layersGrid.setCanAcceptDroppedRecords(true);
		this.layersGrid.setCanReorderRecords(true);
		this.layersGrid.setDragDataAction(DragDataAction.MOVE);

		layerSelectedField = new ListGridField(
				WMSLayerDefRecordMapper.IS_VISIBLE, "Visible");
		layerSelectedField.setType(ListGridFieldType.BOOLEAN);
		layerSelectedField.setCanEdit(true);
		ListGridField nameField = new ListGridField(
				WMSLayerDefRecordMapper.NAME, "Name");
		ListGridField urlField = new ListGridField(WMSLayerDefRecordMapper.URL,
				"URL");
		ListGridField layersField = new ListGridField(
				WMSLayerDefRecordMapper.LAYERS, "Layers");

		this.layersGrid.setFields(layerSelectedField, nameField, urlField,
				layersField);

		addLayerButton = new IButton("Add Layer");
		
		widget.addMember(this.layersGrid);
		widget.addMember(addLayerButton);
		
		return widget;
	}

	@Override
	public HasCellClickHandlers getLayerRemove() {
		return this.layersGrid;
	}

	@Override
	public int getWindowHeight() {
		return 300;
	}

	@Override
	public String getWindowTitle() {
		return "Layers";
	}

	@Override
	public int getWindowWidth() {
		return 400;
	}

	@Override
	public HasValues<WMSLayerDef> getLayers() {
		return new HasValuesListGridAdapter<WMSLayerDef>(this.layersGrid,
				new WMSLayerDefRecordMapper());
	}

	@Override
	public int getRowNumber(Record record) {
		return this.layersGrid.getRecordIndex(record);
	}

	@Override
	public HasClickHandlers getAddLayerClick() {
		return this.addLayerButton;
	}
	
	@Override
	protected void addHeaderControls(Map<String, HasClickHandlers> headers) {
		window.setHeaderControls(HeaderControls.HEADER_LABEL, headers
				.get(WindowedDisplay.MINIMIZE_HEADER));
	}

}
